package com.example.park_management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.park_management.common.PageParam;
import com.example.park_management.common.Result;
import com.example.park_management.domain.Assets;
import com.example.park_management.domain.Buildings;
import com.example.park_management.domain.Floors;
import com.example.park_management.domain.Rooms;
import com.example.park_management.service.BuildingsService;
import com.example.park_management.service.FloorsService;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/buildings")
public class BuildingsController {

    @Resource
    BuildingsService buildingsService;
    @Resource
    FloorsService floorsService;
    @PostMapping("/")
    public Result add(@RequestBody Buildings buildings) {
        try{
            if (StringUtils.isBlank(buildings.getBuildingName()) || StringUtils.isBlank(buildings.getLocation()) || StringUtils.isBlank(buildings.getType())) {
                return Result.fail("必填属性为空");
            }
            boolean b = buildingsService.save(buildings);
            if (b){
                return Result.success();
            }
            return Result.fail();
        }catch (Exception e) {
            return Result.fail("输入格式异常！");
        }
    }

    @DeleteMapping("/{buildingsId}")
    public Result delete(@PathVariable Integer buildingsId) {
        //检验楼栋下面是否有楼层，若有提示不允许删除楼栋应先删除楼层数据
        LambdaQueryWrapper<Floors> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Floors::getBuildingId,buildingsId);
        List<Floors> list = floorsService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(list)){
            String fStr = list.stream().map(f -> f.getFloorNumber().toString()).collect(Collectors.joining());
            return Result.fail("删除失败:"+fStr);
        }
        boolean b = buildingsService.removeById(buildingsId);
        if (b){
            return Result.success();
        }
        return Result.fail();
    }

    @PutMapping("/")
    public Result update(@RequestBody Buildings buildings) {
        try{
            if (StringUtils.isBlank(buildings.getBuildingName()) || StringUtils.isBlank(buildings.getLocation()) || StringUtils.isBlank(buildings.getType())) {
                return Result.fail("必填属性为空");
            }
            boolean b = buildingsService.updateById(buildings);
            if (b){
                return Result.success();
            }
            return Result.fail();
        }catch (Exception e) {
            return Result.fail("输入格式异常！");
        }
    }

    @PostMapping("/page")
    public Result page(@RequestBody PageParam<Buildings> pageParam) {
        Page<Buildings> buildingsPage = new Page<>(pageParam.getPageNum(),pageParam.getPageSize());
        QueryWrapper<Buildings> buildingsQueryWrapper = new QueryWrapper<>();
        if (pageParam.getData() != null){
            if (pageParam.getData().getBuildingName() != null){
                buildingsQueryWrapper.like("building_name", pageParam.getData().getBuildingName());
            }
        }
        buildingsService.page(buildingsPage,buildingsQueryWrapper);
        return Result.success(buildingsPage.getRecords(),buildingsPage.getTotal());
    }
}
